package com.shengyuan.manage_os.old.ying.erpm;

import java.io.File;

import com.shengyuan.manage_os.old.ying.boce.jsy.APIErpm;
import com.shengyuan.manage_os.old.ying.boce.jsy.MobileHandlerTask;
import com.shengyuan.manage_os.old.ying.erpm.data.SharePreferenceUtil;
import com.shengyuan.manage_os.old.ying.erpm.util.CrashHandler;
import com.shengyuan.manage_os.old.ying.erpm.util.SoundControler;
import com.shengyuan.manage_os.old.ying.erpm.util.Utility;
import com.shengyuan.manage_os.utils.DeviceUtils;

import android.app.ActivityManager;
import android.app.AlarmManager;
import android.app.Application;
import android.content.Context;
import android.os.Environment;
import android.support.multidex.MultiDexApplication;
import android.widget.Toast;

public class MainApplication extends MultiDexApplication {
	public static Context Instance; 
	public static BaseActivity currentActivity;
	public static APIErpm mApiErpm = new APIErpm();
	
	public static String AppRoot = Environment.getExternalStorageDirectory().getAbsolutePath()+"/jsy/";
	public static String cameraPath = AppRoot + "carmera_temp/";
	public static String voicePath = AppRoot + "voice_temp/";
	public static String filePath = AppRoot + "file_temp/";
	public static String UploadPath = AppRoot + "uploadfile/";
	public static String updatefile = AppRoot + "update.xml";
	public static String  MapRoot = "map";
	
	public static SharePreferenceUtil pShareUtil;
	public static MobileHandlerTask mobileHandlerTask ;
	public static AlarmManager mAlarmManager ;
	public static SoundControler mSoundControler ;
	
	public static Boolean APPDEBUG = false;
	@Override
	public void onCreate() {
		//KillPackTask();
		super.onCreate();
		Instance = this;
		System.out.println("#############MainApplication");
		pShareUtil =  new SharePreferenceUtil(getApplicationContext());
		mobileHandlerTask = new MobileHandlerTask();
	}

	@Override
	public void onTerminate() {
		super.onTerminate();
		mobileHandlerTask.stop();
	}
	public static void CheckDirectory() {

		if (Utility.isExistExternalStore()) {
			AppRoot = Environment.getExternalStorageDirectory().getAbsolutePath() + "/jsy/";
		}else{
			AppRoot = Instance.getFilesDir().getPath() + "/jsy/";
		}
		cameraPath = AppRoot + "camera_temp/";
		voicePath =  AppRoot + "voice_temp/";
		filePath = AppRoot + "file_temp/";
		MapRoot = AppRoot + "map_temp/";
		UploadPath = AppRoot + "uploadfiles/";
		MapRoot = AppRoot+"map/";
		File file = new File(AppRoot);
		if (!file.exists() && !file.isDirectory()) {
			file.mkdirs();
//				Toast.makeText(this, "拍照目录没有建立,不能拍照!", Toast.LENGTH_SHORT)
//						.show();
		}
		file = new File(cameraPath);
		if (!file.exists() && !file.isDirectory()) {
			file.mkdirs();
//				Toast.makeText(this, "本地目录没有建立,不能拍照!", Toast.LENGTH_SHORT)
//						.show();
		}
		file = new File(voicePath);
		if (!file.exists() && !file.isDirectory()) {
			file.mkdirs();
//				Toast.makeText(this, "本地目录没有建立,不能录音!", Toast.LENGTH_SHORT)
//						.show();
		}
		file = new File(filePath);
		if (!file.exists() && !file.isDirectory()) {
			file.mkdirs();
//				Toast.makeText(this, "本地数据目录没有建立!", Toast.LENGTH_SHORT)
//						.show();
		}			
		file = new File(UploadPath);
		if (!file.exists() && !file.isDirectory()) {
			file.mkdirs();
//				Toast.makeText(this, "本地数据目录没有建立!", Toast.LENGTH_SHORT)
//						.show();
		}			

	}
	
	private void KillPackTask() {
		ActivityManager am = (ActivityManager) this
				.getSystemService(Context.ACTIVITY_SERVICE);
		String packageDocumentsToGoName = getPackageName(); // "com.ying.weixin";
		am.killBackgroundProcesses(packageDocumentsToGoName);
	}	
}
